﻿using AutoMapper;
using Common.Property;
using Common.Utility;
using DapperExtensions;
using DapperExtensions.Predicate;
using Domain.Sys;
using DTO;
using DTO.DTO.Sys.AdminUser;
using IRepository.Sys;
using IServices.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Services.Sys
{
    public class AdminUserService : IAdminUserService
    {
        private IMapper _mapper;
        private IAdminUserRepository _adminUserRepository;
        public AdminUserService(IMapper mapper, IAdminUserRepository adminUserRepository)
        {
            _mapper = mapper;
            _adminUserRepository = adminUserRepository;
        }

        public async Task<Result> Save(AdminUserDetailDTO dto, OperatorInfo op)
        {
            if (string.IsNullOrWhiteSpace(dto.Id))
            {
                var checkExists = await _adminUserRepository.GetByUserNameAsync(dto.UserName);
                if (checkExists != null)
                {
                    return Result.Failed(-1, "该用户名已存在");
                }

                var model = _mapper.Map<AdminUser>(dto);
                model.Password = SHAEncrypt.SHA256(dto.Password);
                var status = await _adminUserRepository.InsertAsync(model, op);

                return status ? Result.Success("保存成功", model.Id) : Result.Failed(-1, "保存失败");
            }
            else
            {
                var checkExists = await _adminUserRepository.GetByUserNameAsync(dto.UserName);
                if (checkExists != null && checkExists.Id != dto.Id)
                {
                    return Result.Failed(-1, "该用户名已存在");
                }

                var model = await _adminUserRepository.GetByIdAsync(dto.Id);
                _mapper.Map(dto, model);
                if (dto.Password != Const.SecretStr)
                {
                    model.Password = SHAEncrypt.SHA256(dto.Password);
                }
                var status = await _adminUserRepository.UpdateAsync(model, op);
                return status ? Result.Success("保存成功", model.Id) : Result.Failed(-1, "保存失败");
            }
        }

        public async Task<bool> Delete(string id, OperatorInfo op)
        {
            return await _adminUserRepository.DeleteAsync(id, op);
        }

        public async Task<bool> DeleteBatch(List<string> ids, OperatorInfo op)
        {
            return await _adminUserRepository.DeleteBatchAsync(ids, op);
        }

        public async Task<AdminUser> GetById(string id)
        {
            return await _adminUserRepository.GetByIdAsync(id);
        }

        public async Task<AdminUser> GetByUserNameAsync(string username)
        {
            return await _adminUserRepository.GetByUserNameAsync(username);
        }

        public async Task<Tuple<IEnumerable<AdminUser>, int>> GetPageList(AdminUserQueryDTO dto)
        {
            var pgb = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
            if (!string.IsNullOrWhiteSpace(dto.Keywords))
            {
                pgb.Predicates.Add(Predicates.Field<AdminUser>(f => f.UserName, Operator.Like, $"%{dto.Keywords}%"));
            }
            var result = await _adminUserRepository.GetPageListAsync(dto.PageIndex, dto.PageSize, pgb);
            return result;
        }

        public async Task<Result> SavePassword(AdminUserPasswordDTO dto, OperatorInfo op)
        {
            var model = await _adminUserRepository.GetByIdAsync(dto.Id);

            if (SHAEncrypt.SHA256(dto.OldPassword) != model.Password)
            {
                return Result.Failed(-1, "旧密码不正确");
            }
            model.Password = SHAEncrypt.SHA256(dto.Password);
            var status = await _adminUserRepository.UpdateAsync(model, op);
            return status ? Result.Success("保存成功", model.Id) : Result.Failed(-1, "保存失败");
        }
    }
}
